//
//  MYY_LoginTextField.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/26.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYY_LoginTextField.h"

#import "Stalker.h"

#define kTextFieldHeight 50

typedef void(^KeyboardBlock)();

typedef void(^UserSelectBlock)();

@interface MYY_LoginTextField()<UITextFieldDelegate>

@property(nonatomic, strong) UIImageView *iconView;
@property(nonatomic, strong) UIButton *clearButton;
@property(nonatomic, strong) UIView *seperatorView;

@property(nonatomic, copy) NSString *normalIcon;
@property(nonatomic, copy) NSString *highlightIcon;
@property(nonatomic, assign) BOOL needDropDown;
@property(nonatomic, assign) BOOL seperator;

@property(nonatomic, strong) STStalker *stalker;
@property(nonatomic, copy) UserSelectBlock userSelectBlock;

@property(nonatomic, copy) KeyboardBlock nextBlock;
@property(nonatomic, copy) KeyboardBlock doneBlock;
@property(nonatomic, copy) KeyboardBlock clearBlock;

@end
@implementation MYY_LoginTextField
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.iconView];
        [self addSubview:self.textField];
        //[self addSubview:self.dropDownButton];
        [self addSubview:self.clearButton];
        [self addSubview:self.seperatorView];
        
        WS(weakSelf)
        [self.stalker when:UITextFieldTextDidChangeNotification then:^(NSNotification *notification) {
            [weakSelf highlight:[weakSelf.textField isEditing]];
        }];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.iconView.frame = CGRectMake(25, 0, 18, 50);
    
    if (self.needDropDown) {
        self.dropDownButton.hidden = NO;
        if (iphone6 || iphone6P) {
            self.dropDownButton.frame = CGRectMake(WIDTH - 45, (CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
            self.clearButton.frame = CGRectMake(WIDTH-45,(CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
        }else if (IS_IPHONE_X){
            self.dropDownButton.frame = CGRectMake(WIDTH - 75, (CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
            self.clearButton.frame = CGRectMake(WIDTH-75,(CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
        }
        else {
            if (self.clearButton.hidden) {
                self.dropDownButton.frame = CGRectMake(WIDTH - 45-5, (CGRectGetHeight(self.bounds) - 40)/2.0, 40, 40);
                self.clearButton.frame = CGRectMake(WIDTH-45-32-5-80,(CGRectGetHeight(self.bounds) - 40)/2.0, 40, 40);
            }
            else {
                self.dropDownButton.frame = CGRectMake(WIDTH - 45, (CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
                self.clearButton.frame = CGRectMake(WIDTH-25,(CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
            }
        }
    }
    else {
        self.dropDownButton.hidden = YES;
        if (iphone6 || iphone6P) {
            self.clearButton.frame = CGRectMake(WIDTH - 25-110, (CGRectGetHeight(self.bounds) - 31)/2.0, 31, 31);
        }
        else {
            self.clearButton.frame = CGRectMake(WIDTH - 25-40-110, (CGRectGetHeight(self.bounds) - 40)/2.0, 40, 40);
        }
    }
    CGFloat sideSpace  = 0;
    if (iphone5S) {
        sideSpace = 30;
    }
    self.textField.frame = CGRectMake(CGRectGetMaxX(self.iconView.frame)+15, 0, CGRectGetMinX(self.clearButton.frame) - 10 - CGRectGetMaxX(self.iconView.frame)-20 + sideSpace, kTextFieldHeight);
    self.seperatorView.frame = CGRectMake(CGRectGetMaxX(self.iconView.frame)+15, CGRectGetHeight(self.bounds)-0.5, WIDTH-(CGRectGetMaxX(self.iconView.frame)+15+25), 0.5);
}

- (void)setNormalIcon:(NSString *)normalIcon highlightIcon:(NSString *)highlightIcon placeholder:(NSString *)placeholder password:(BOOL)password returnKeyType:(UIReturnKeyType)returnKeyType dropDown:(BOOL)dropDown seperator:(BOOL)seperator userSelectBlock:(void(^)())userSelectBlock clearBlock:(void(^)())clearBlock nextBlock:(void(^)())nextBlock doneBlock:(void(^)())doneBlock
{
    self.normalIcon = normalIcon;
    self.highlightIcon = highlightIcon;
    self.needDropDown = dropDown;
    self.seperator = seperator;
    self.userSelectBlock = userSelectBlock;
    self.nextBlock = nextBlock;
    self.doneBlock = doneBlock;
    self.clearBlock = clearBlock;
    self.iconView.image = [UIImage imageNamed:self.normalIcon];
    self.textField.placeholder = placeholder;
    self.textField.returnKeyType = returnKeyType;
    self.textField.secureTextEntry = password?YES:NO;
  //  self.seperatorView.hidden = !seperator;
    [self setNeedsLayout];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self highlight:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self highlight:NO];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 10000) {
        self.nextBlock();
    }
    else {
        self.doneBlock();
    }
    return YES;
}

- (void)highlight:(BOOL)hightLight
{
    self.iconView.image = [UIImage imageNamed:hightLight?self.highlightIcon:self.normalIcon];
    if (![self.textField.text isEqualToString:@""] && hightLight) {
        self.clearButton.hidden = NO;
        [self updateSelectState:NO];
        
        [self setNeedsLayout];
    }
    else {
        self.clearButton.hidden = YES;
        [self updateSelectState:YES];
        
        [self setNeedsLayout];
    }
}

- (void)updateSelectState:(BOOL)selected
{
    //    if (selected) {
    //        [_dropDownButton setImage:[UIImage imageNamed:((iphone6||iphone6P)?@"login_ic_up":@"login_ic_up_5s")] forState:UIControlStateNormal];
    //    }
    //    else {
    //        [_dropDownButton setImage:[UIImage imageNamed:((iphone6||iphone6P)?@"login_ic_down":@"login_ic_down_5s")] forState:UIControlStateNormal];
    //    }
}

- (void)clearTextTapEvent
{
    self.clearBlock();
}

- (void)selectUsersTapEvent
{
    self.userSelectBlock();
    self.dropDownButton.selected = !self.dropDownButton.selected;
    [self.textField resignFirstResponder];
}

#pragma mark - property
- (UIImageView *)iconView
{
    if (_iconView == nil) {
        _iconView = [UIImageView new];
        _iconView.contentMode = UIViewContentModeCenter;
    }
    return _iconView;
}

- (UITextField *)textField
{
    if (_textField == nil) {
        _textField = [UITextField new];
        _textField.textColor = [UIColor whiteColor];
        _textField.keyboardType = UIKeyboardTypeDefault;
        _textField.font = [UIFont systemFontOfSize:15];
       
        _textField.delegate = self;
    }
    return _textField;
}

- (UIButton *)dropDownButton
{
    if (_dropDownButton == nil) {
        _dropDownButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dropDownButton setImage:[UIImage imageNamed:(@"xiala")] forState:UIControlStateNormal];
        _dropDownButton.hidden = YES;
        [_dropDownButton addTarget:self action:@selector(selectUsersTapEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dropDownButton;
}

- (UIButton *)clearButton
{
    if (_clearButton == nil) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_clearButton setImage:[UIImage imageNamed:(@"chahaozu")] forState:UIControlStateNormal];
        _clearButton.hidden = YES;
        [_clearButton addTarget:self action:@selector(clearTextTapEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}

- (UIView *)seperatorView
{
    if (_seperatorView == nil) {
        _seperatorView = [UIView new];
        _seperatorView.backgroundColor = [UIColor whiteColor];
    }
    return _seperatorView;
}

- (STStalker *)stalker
{
    if (_stalker == nil) {
        _stalker = [STStalker new];
    }
    return _stalker;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
